package com.sky.module.system.heart.handle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.module.system.heart.dao.HeartMapper;
import com.sky.module.system.heart.domain.entity.HeartEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/***
 * @description 心跳服务持久化处理类
 * @param null
 * @return
 * @date 2023/3/11 15:34
 *
 */
@Component
public class HeartBeatRecordHandler {
    @Autowired
    private HeartMapper heartMapper;

    public void handler(HeartEntity heartEntity) {
        LambdaQueryWrapper<HeartEntity> wrapper = new LambdaQueryWrapper<>();
        // 进程号
        wrapper.eq(HeartEntity::getProcessNo, heartEntity.getProcessNo());
        // 服务ip
        wrapper.eq(HeartEntity::getServerIp, heartEntity.getServerIp());

        // 项目路径
        wrapper.eq(HeartEntity::getProjectPath, heartEntity.getProjectPath());

        HeartEntity heartEntity1 = heartMapper.selectOne(wrapper);

        if (heartEntity1 == null) {
//         做新增操作
            heartMapper.insert(heartEntity);
        } else {
            heartEntity.setHeartBeatTime(LocalDateTime.now());
            heartMapper.updateById(heartEntity);
            System.out.println("心跳检测");
        }
    }
}
